package shun.demo.remote.display.client;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.os.*;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import shun.demo.remote.display.model.Movement;
import shun.demo.remote.display.model.Property;

/**
 * Created by chen on 14-5-9.
 */
public class XScreenHandler extends Handler {

    public static final int MESSAGE_SEND = 1;
    public static final int MESSAGE_MOVEMENT = 2;

    private View mTarget;

    private static XScreenHandler sInstance;

    public static XScreenHandler get() {
        if (sInstance == null) {
            synchronized (XScreenHandler.class) {
                if (sInstance == null) {
                    sInstance = new XScreenHandler();
                }
            }
        }
        return sInstance;
    }

    private XScreenHandler() {
        super(Looper.getMainLooper());
    }

    private ValueAnimator mAnimator;
    private ValueAnimator mAnimatorX;
    private ValueAnimator mAnimatorY;
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void handleMessage(Message msg) {
        long now = SystemClock.uptimeMillis() + XScreen.get().getTimeOffset();
        long latency;
        switch (msg.what) {
            case MESSAGE_SEND:
                Property p = (Property) msg.obj;
                Log.i("XScreenHandler", "from thread " + "\n" + p.toString());
                latency = now - p.getTime();
                Log.i("XScreenHandler", "latency: " + latency);
                if (mTarget != null) {
                    mTarget.setX(p.getX());
                    if (mAnimator != null && mAnimator.isRunning()) {
                        mAnimator.cancel();
                    }
                    mAnimator = ObjectAnimator.ofFloat(mTarget, "x", 0, 1500.0f);
                    mAnimator.setInterpolator(new LinearInterpolator());
                    mAnimator.setDuration(5000);
                    mAnimator.setCurrentPlayTime(latency);
                    mAnimator.start();
                    Log.i("XScreenHandler", mAnimator.toString());
                }
                break;
            case MESSAGE_MOVEMENT:
                Movement movement = (Movement) msg.obj;
                Log.i("XScreenHandler", "from thread " + "\n" + movement.toString());
                latency = now - movement.getTime();
                Log.i("XScreenHandler", "latency: " + latency);
                if (mTarget != null) {
                    if (mAnimatorX != null && mAnimatorX.isRunning()) {
                        mAnimatorX.cancel();
                    }
                    if (mAnimatorY != null && mAnimatorY.isRunning()) {
                        mAnimatorY.cancel();
                    }
                    mAnimatorX = movement.createAnimatorXFor(mTarget);
                    mAnimatorY = movement.createAnimatorYFor(mTarget);
                    mAnimatorX.setCurrentPlayTime(latency);
                    mAnimatorY.setCurrentPlayTime(latency);
                    mAnimatorX.start();
                    mAnimatorY.start();
                    Log.i("XScreenHandler", mAnimatorX.toString());
                    Log.i("XScreenHandler", mAnimatorY.toString());
                }
                break;
        }
    }

    public void setTarget(View target) {
        mTarget = target;
    }

    public View getTarget() {
        return mTarget;
    }
}
